package com.cn.kabalifeuser.ui.my.fragment;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.TimePickerView;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.cn.kabalifeuser.R;
import com.cn.kabalifeuser.base.LazyFragment;
import com.cn.kabalifeuser.config.CustomRefreshListener;
import com.cn.kabalifeuser.config.LoginHelper;
import com.cn.kabalifeuser.config.RefreshHelper;
import com.cn.kabalifeuser.ui.my.activity.IntegralListPresenter;
import com.cn.kabalifeuser.ui.my.activity.IntegralListView;
import com.cn.kabalifeuser.ui.my.bean.IntegralListBean;
import com.cn.kabalifeuser.utils.LoggerUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by 晓林 on 2020/10/30.
 * function:
 */
public class IntegralListFragments extends LazyFragment<IntegralListView, IntegralListPresenter> implements IntegralListView {
    @BindView(R.id.rv_notice)
    RecyclerView recyclerView;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.tv_time)
    TextView tvTime;
    @BindView(R.id.tv_num_all)
    TextView tvNumAll;
    private int mId;

    List<IntegralListBean.ResultBean.DataBean> mList;
    IntegralListAdapter mAdapter;


    //时间
    TimePickerView pvTime;
    String time = "";

    String year = "";
    String month = "";

    public static IntegralListFragments getInstance(int mid) {
        IntegralListFragments fragment = new IntegralListFragments();
        fragment.mId = mid;
        return fragment;
    }

    @Override
    protected IntegralListPresenter createPresenter() {
        return new IntegralListPresenter();
    }

    @Override
    protected void onUserVisible() {
        if (mList != null && mList.size() == 0) {
            pagehttp = 1;
            getListData();
        }
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.fragment_integral_list;
    }

    @Override
    public void initView(View view) {
        super.initView(view);
        ButterKnife.bind(this, view);

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");// HH:mm:ss
        //获取当前时间
        Date date = new Date(System.currentTimeMillis());
        tvNumAll.setText("0");
        tvTime.setText(simpleDateFormat.format(date));

        mList = new ArrayList<>();
        LinearLayoutManager layoutManager3 = new LinearLayoutManager(getMe());
        layoutManager3.setOrientation(LinearLayoutManager.VERTICAL);
        layoutManager3.setSmoothScrollbarEnabled(true);
        layoutManager3.setAutoMeasureEnabled(true);
        recyclerView.setLayoutManager(layoutManager3);
        recyclerView.setNestedScrollingEnabled(false);
        //创建适配器
        mAdapter = new IntegralListAdapter(R.layout.fragment_integral_item, mList, mId);
        //给RecyclerView设置适配器
        recyclerView.setAdapter(mAdapter);
        initRefreshLayout();

    }

    /**
     * 选择时间
     */
    private void showTimeSelector() {
        Calendar selectedDate = Calendar.getInstance();
        Calendar startDate = Calendar.getInstance();
        //startDate.set(2013,1,1);
        Calendar endDate = Calendar.getInstance();
        //endDate.set(2020,1,1);
        //正确设置方式 原因：注意事项有说明
        startDate.set(2000, 1, 1);
        endDate.set(2230, 11, 28);
        pvTime = new TimePickerBuilder(getMe(), new OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(date);
//                timeUp = StringUtil.getTimeAllFormat(String.valueOf(date.getTime()));
//                tvTime.setText(timeUp);
                //获取年
                year = String.valueOf(calendar.get(Calendar.YEAR));
                //获取月份，0表示1月份
                month = String.valueOf((calendar.get(Calendar.MONTH) + 1));
                LoggerUtils.e("选择的年：" + year + "-" + month);
                tvTime.setVisibility(View.VISIBLE);
                tvTime.setText(year + "-" + month);
                time = year + "-" + month;
                refreshLayout.autoRefresh();

            }
        })
                .setType(new boolean[]{true, true, false, false, false, false})
                .setSubmitText(getResources().getString(R.string.main_sure))
                .setCancelText(getResources().getString(R.string.main_cancel))
                .isCyclic(true)
                .setSubmitColor(getResources().getColor(R.color.main_color))//确定按钮文字颜色
                .setCancelColor(getResources().getColor(R.color.font_black))//取消按钮文字颜色
                .setTitleBgColor(getResources().getColor(R.color.main_white))
                .setDate(selectedDate)// 如果不设置的话，默认是系统时间*/
                .setRangDate(startDate, endDate)//起始终止年月日设定
                .setLabel(getResources().getString(R.string.time_year_str), getResources().getString(R.string.time_month_str), getResources().getString(R.string.time_day_str), getResources().getString(R.string.time_hours_str), getResources().getString(R.string.time_mins_str), getResources().getString(R.string.time_second_str))//默认设置为年月日时分秒
                .isCenterLabel(false) //是否只显示中间选中项的label文字，false则每项item全部都带有label。
                .isDialog(false).build();
    }

    /**
     * 刷新
     */
    public void initRefreshLayout() {
        RefreshHelper.initRefreshLayout(refreshLayout, new CustomRefreshListener() {
            @Override
            public void _onRefresh(RefreshLayout refreshLayout) {
                pagehttp = 1;
                getListData();
            }

            @Override
            public void _onLoadMore(RefreshLayout refreshLayout) {
                pagehttp++;
                if (null != mList) {
                    getListData();
                }
            }
        });
    }

    private void getListData() {
        mPresenter.onListData(LoginHelper.getLonginData().getResult(), mId, time, pagehttp, pageNum);
    }

    @Override
    public void onReLoggedIn(String msg) {

    }

    @Override
    public void onError(String msg) {

    }

    @Override
    public Context _getContext() {
        return null;
    }

    @Override
    public void onListSuccess(IntegralListBean bean) {
        tvNumAll.setText(String.valueOf(bean.getResult().getIntegral_sum()));
        RefreshHelper.finishRefresh(refreshLayout, pagehttp);
        if (pagehttp == 1) {
            if (bean.getResult() != null && bean.getResult().getData().size() > 0) {
                mAdapter.replaceData(bean.getResult().getData());
            } else {
                mAdapter.replaceData(bean.getResult().getData());
            }
        } else {
            if (bean.getResult() != null && bean.getResult().getData().size() > 0) {
                mAdapter.addData(bean.getResult().getData());
            }

        }
    }

    @OnClick(R.id.ll_btn_time)
    public void onViewClicked() {
        if (pvTime != null) {
            pvTime.show();
        } else {
            showTimeSelector();
            pvTime.show();
        }
    }

    class IntegralListAdapter extends BaseQuickAdapter<IntegralListBean.ResultBean.DataBean, BaseViewHolder> {


        int mid;

        public IntegralListAdapter(int layoutResId, @Nullable List<IntegralListBean.ResultBean.DataBean> data, int mid) {
            super(layoutResId, data);
            this.mid = mid;
        }

        @Override
        protected void convert(BaseViewHolder helper, final IntegralListBean.ResultBean.DataBean item) {
            helper.setText(R.id.content, item.getContent())
                    .setText(R.id.created_at, item.getCreated_at())
                    .setText(R.id.integral, mid == 1 ? "+"+ item.getIntegral() : "-" + item.getIntegral());


        }
    }
}

